using System.Collections.Generic;
using Cinemachine;
using Data;
using Framework.FSMLite;
using Framework.GamePool;
using Framework.Utils.SingletonTemplate;
using GameSystem;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;

public class GameManager : SingletonMonoBase<GameManager>
{
    public StateMachineRunner<GameEnum> GameFsm { get; private set; }
    public GameObject GamePoolObject { get; private set; }
    public Transform World { get; private set; }

    public GameRoom GameRoom { get; private set; }
    
    public Camera MainCamera;

    public bool IsGaming;
    
    private Dictionary<GameLevel , CinemachineVirtualCameraBase[]> _virtualCameras;
    
    public void InitGame()
    {
        this.GameRoom = new GameRoom();
        this.MainCamera = Camera.main;
        this.World = GameObject.Find("World").transform;
        this.GamePoolObject = GameObject.Find("GamePoolObject");
        this._virtualCameras = new Dictionary<GameLevel, CinemachineVirtualCameraBase[]>();
        this._virtualCameras[GameLevel.Level3] = GameObject.Find("VCam3").GetComponentsInChildren<CinemachineVirtualCameraBase>();
        this._virtualCameras[GameLevel.Level5] = GameObject.Find("VCam5").GetComponentsInChildren<CinemachineVirtualCameraBase>();
        this._virtualCameras[GameLevel.Level9] = GameObject.Find("VCam9").GetComponentsInChildren<CinemachineVirtualCameraBase>();
        GamePoolMgr.Init();
        this.GameFsm = new StateMachineRunner<GameEnum>();
#if UNITY_EDITOR
        // this.GameFsm.OnStateEnter += @enum => { Debug.LogError("进入节点： " + @enum); };
#endif
        this.GameFsm
            .AppendState<StateInit>(GameEnum.Init)
            .AppendState<StateHome>(GameEnum.Home)
            .AppendState<StateGame>(GameEnum.Game)
            .AppendState<StateLoading>(GameEnum.Loading)
            .AppendState<StateWin>(GameEnum.Win);
        this.GameFsm.Active(GameEnum.Init);
    }

    public void ChangeVCam(int index , GameLevel gameLevel = GameLevel.Level5)
    {
        this._virtualCameras[gameLevel][index].MoveToTopOfPrioritySubqueue();
    }

    public static Vector3 ToV3(Vector2 vector2 , float Y = 0)
    {
        return new Vector3(vector2.x , Y , vector2.y);
    }

}